<?php
/*******************************************************************************
 * $Date:: Jan 7, 2011 9:45:36 PM                                              $
 * $Revision:                                                                  $
 * $Author:: Tuan Duong                                                        $
 * $Comment::                                                                  $
 ******************************************************************************/
/**
 * Description here...
 *
 * @package Lua
 * @subpackage Controller
 * @author Tuan Duong <bacduong[at]gmail[dot]com>
 * @version
 */

class UserController extends Lua_Controller_Admin
{
    /**
     * Index Action
     */
    public function loginAction()
    {
        $this->view->disableLayout();
    }

    public function loginSubmitAction()
    {
        $request = Lua_Request::getInstance();
        $uri = Lua_Uri::getInstance();
        $username = $request->getPost('username', '');
        if ('' != $username) {
            $password = $request->getPost('password', '');
            $auth = new Lua_Auth_User($username, $password);
            $result = Zend_Auth::getInstance()->authenticate($auth);
            if ($result->isValid()) {
                $this->redirect(array(
                    Lua_Constant_General::PARAM_CONTROLLER => 'index',
                    Lua_Constant_General::PARAM_ACTION => 'index'
                ));
            } else {
                $this->forward(array(
                    Lua_Constant_General::PARAM_ACTION => 'login', 
                    Lua_Constant_General::PARAM_CONTROLLER => 'user'
                ));
                return;
            }
        } else {
            $this->forward(array(Lua_Constant_General::PARAM_ACTION => 'login'));
        }
    }
    

    public function logoutAction()
    {
        Zend_Auth::getInstance()->clearIdentity();
        $this->redirect(array(
            Lua_Constant_General::PARAM_MODULE => 'admin',
            Lua_Constant_General::PARAM_CONTROLLER => 'user',
            Lua_Constant_General::PARAM_ACTION => 'login'
        ));
    }
    
    public function indexAction()
    {
        $params = Lua_Uri::getInstance()->getQueryAsArray();
        $userService = Lua_Service_Locator::getUserService();
        $select = $userService->getUserSelect($params);
        
        // Initialize grid
        $grid = new Lua_Grid($select, array('cssClass' => 'adminlist'));
        $grid->setView($this->view->getAdapter());
        $grid->setParams($params);
        $columns = array(
            new Lua_Grid_Column_Input(Lua_Grid_Column_Input::CHECKBOX, 'id'),
            'id',
            new Lua_Grid_Column_Value('username', array('sortable' => true)),
            new Lua_Grid_Column_Value('full_name'),
            new Lua_Grid_Column_Value('email'),
            new Lua_Grid_Column_Date('created_time', array('title' => "Registered date")),
        );
        
        $grid->buildColumns($columns);
        $grid->deploy();
        $this->view->assign('grid', $grid);
        
        $baseService = Lua_Service_Locator::getBaseService();
        $groups = $baseService->findAll('ipoo_group', array('id != ?' => Lua_Constant_Database::GUEST_GROUP_ID));
        $groupSelect = array('' => 'All');
        foreach ($groups as $group) {
            $groupSelect[$group['id']] = $group['name']; 
        }
        
        if (!isset($params['group_id'])) {
            $params['group_id'] = '';
        }
        
        $this->view->assign("params", $params);
        $this->view->assign("groups", $groupSelect);
    }

    public function addAction() {
    }
    
    public function addSubmitAction() {
    	$uri = Lua_Uri::getInstance();
    	$request = Lua_Request::getInstance();
    	$pressbutton = $request->getPost('pressbutton', 'cancel');
    	if($pressbutton == 'cancel') {
    		$this->forward(array(
    		    Lua_Constant_General::PARAM_ACTION => 'index', 
    		    Lua_Constant_General::PARAM_CONTROLLER => 'user'
    		));
    	} else {
    		$userService = Lua_Service_Locator::getUserService();
    		if($pressbutton == 'save') {
    			$ipooUser = new Lua_Bean_IpooUser($_REQUEST);
    			$errors = $this->validateAddUser($ipooUser);
    			if(sizeof($errors) == 0) {
	    			//insert data then goto admin home page
	    			$userService->addUser($ipooUser, $_POST["gid"]);
	    			$this->forward(array(
	    			    Lua_Constant_General::PARAM_ACTION => 'index', 
	    			    Lua_Constant_General::PARAM_CONTROLLER => 'user'
	    			));
    			} else {
    				$this->forward(array(
    				    Lua_Constant_General::PARAM_ACTION => 'add', 
    				    Lua_Constant_General::PARAM_CONTROLLER => 'user'
    				));
    				$this->view->assign('errors', $errors);
    			}
    		} 
    	}    	
    	
    }
    

    public function editAction() {
        $ids = Lua_Request::getInstance()->getParam('id', '');
    	if (!empty($ids)) {
    		$id = $ids[0];
    		$userService = Lua_Service_Locator::getUserService();
    		$existingUsers = $userService->findUserByProperty('id', $id);
    		if (sizeof($existingUsers) == 1) {
    			$updatedUser = $existingUsers[0];
    			$groupIds = $userService->getGroupIds($id);
    			$this->view->assign('updatedUser', $updatedUser);
    			$this->view->assign('groupIds', $groupIds);
    		}
    	}
    }
    

    public function deleteAction() {
        $ids = Lua_Request::getInstance()->getParam('id', array());
		$userService = Lua_Service_Locator::getUserService();
		foreach ($ids as $id) {
			$existingUsers = $userService->deleteUser($id);
		}
    	$this->forward(array(
    	    Lua_Constant_General::PARAM_ACTION => 'index', 
    	    Lua_Constant_General::PARAM_CONTROLLER => 'user'
    	));
    }
    
 	public function editSubmitAction() {
 	    $request = Lua_Request::getInstance();
 	    $id = $request->getParam('id', '');
    	if (!empty($id)) {
    		$userService = Lua_Service_Locator::getUserService();
    		$existingUsers = $userService->findUserByProperty('id', $id);
    		if (sizeof($existingUsers) == 1) {
    			//update data then goto admin home page
    			$ipooUser = new Lua_Bean_IpooUser($_REQUEST);
    			$userService->updateUser($ipooUser, $request->getPost("gid"));
    			$this->forward(array(Lua_Constant_General::PARAM_ACTION => 'index', Lua_Constant_General::PARAM_CONTROLLER => 'user'));
    		}
    	}
    }
}